package cn.com.dashihui.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.kit.CommonKit;
import cn.com.dashihui.kit.DatetimeKit;
import cn.com.dashihui.kit.DoubleKit;
import cn.com.dashihui.kit.ExcelExportUtil.Pair;
import cn.com.dashihui.kit.SMSKit;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.AdminSeller;
import cn.com.dashihui.web.dao.AuthDownload;
import cn.com.dashihui.web.dao.Seller;
import cn.com.dashihui.web.dao.Store;
import cn.com.dashihui.web.dao.User;
import cn.com.dashihui.web.service.AuthDownloadService;
import cn.com.dashihui.web.service.UserService;

@RequiresAuthentication
public class UserController extends BaseController {
	private UserService service = new UserService();
	private AuthDownloadService downloadService=new AuthDownloadService();
	
	/**
	 * 团队详情
	 */
	public void showTeam(){
		int id = getParaToInt("id", 0);
		int userid = getCurrentUser().getInt("id");
		int RoleId = service.findByRoleId(userid);
		setAttr("RoleId", RoleId);
		int VipPower = 0;
		if (RoleId == 1) {
			VipPower = 1;
			
		} else if (RoleId == 2) {
			if(StrKit.notNull(service.findByVipRole(userid))){
				VipPower = service.findByVipRole(userid).get("VipPower");
				List<Store> storeList = service.findStoreList(userid);
				setAttr("storeList", storeList);
			}
		} else{
			if(StrKit.notNull(service.findByVipRole(userid))){
				VipPower = service.findByVipRole(userid).get("VipPower");
				List<Store> storeList = service.findOneStore(userid);
				setAttr("storeList", storeList);
			}
		}
		/*List<AdSeller> findAdSeller = service.findAdSeller();
		setAttr("findAdSeller", findAdSeller);*/
		List<Store> storeList = service.findStoreList();
		setAttr("storeList", storeList);
		setAttr("id", id);
		setAttr("VipPower", VipPower);
		render("showTeam.jsp");
	}
	public void teampage() {
		//用户id
		int userid = getCurrentUser().getInt("id");
		Seller adminSeller = service.findSellerByAdminId(userid);
		//会员id
		int id = getParaToInt("id", 0);
		int pageNum = getParaToInt(0, 1);
		String phone = getPara("phone");
		String card = getPara("card");
		String storeid=getPara("store");
		String taici=getPara("taici");
		String state=getPara("state");
		int toBossTwoId = 0;
		if (StrKit.notBlank(storeid)) {
			Store store = service.findStoreById(storeid);
			toBossTwoId = store.get("id");
		}
		int toBossOneId = 0;
		String dao=getPara("dao");
		if (StrKit.notBlank(dao)) {
			AdminSeller admin = service.findAdSellerByName(dao);
			toBossOneId = admin.get("id");
		}
		int toBossThreeId = adminSeller.get("id");	
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0, service.findByPageteam(id,pageNum, pageSize, card, phone, taici, state, toBossOneId, toBossTwoId, toBossThreeId));
	}
	/**
	 * 会员信息
	 */
	public void index() {
		int userid = getCurrentUser().getInt("id");
		int RoleId = service.findByRoleId(userid);
		setAttr("RoleId", RoleId);
		int VipPower = 0;
		if (RoleId == 1) {
			VipPower = 1;
			List<Store> storeList = service.findStoreList();
			setAttr("storeList", storeList);
		} else if (RoleId == 2) {
			if(StrKit.notNull(service.findByVipRole(userid))){
				VipPower = service.findByVipRole(userid).get("VipPower");
				List<Store> storeList = service.findStoreList(userid);
				setAttr("storeList", storeList);
			}
		} else{
			if(StrKit.notNull(service.findByVipRole(userid))){
				VipPower = service.findByVipRole(userid).get("VipPower");
				List<Store> storeList = service.findOneStore(userid);
				setAttr("storeList", storeList);
			}
		}
		/*List<AdSeller> findAdSeller = service.findAdSeller();
		setAttr("findAdSeller", findAdSeller);*/
		setAttr("VipPower", VipPower);
		render("index.jsp");
	}
	public void page() {
		AdminSeller adminSeller = getCurrentUser();
		if (adminSeller==null) {
			renderFailed();
			return;
		}
		int userid = adminSeller.getInt("id");
		//获取公司根据用户id
		int sellerId = 0;
		if (adminSeller.get("storeid") == null) {
			Seller seller = service.findSellerByUserId(userid);
			sellerId = seller.get("id");
		}
		if (adminSeller.get("storeid") != null) {
			String storeid = adminSeller.get("storeid");
			Store store = service.findSellerByStoreid(storeid);
			String sellerId2 = store.get("sellerId");
			Seller seller = service.findselleridBysellerId(sellerId2);
			sellerId = seller.get("id");
		}
		//公司主键id
		
		
		int pageNum = getParaToInt(0, 1);
		String phone = getPara("phone");
		String card = getPara("card");
		String sBeginDate = getPara("sBeginDate");// 生日
		String sEndDate = getPara("sEndDate");
		String storeid=getPara("store");
		String dao=getPara("dao");
		int toBossOneId = 0;
		int toBossTwoId = 0;
		int toBossThreeId = 0;
		int roleid = service.findRoleidByAdminSellerId(userid);
		if (roleid == 2) {
			toBossThreeId = sellerId;
			if (StrKit.notBlank(storeid)) {
				toBossTwoId = service.findStoreId(storeid);
			}
			if (StrKit.notBlank(dao)) {
				AdminSeller findAdSellerByName = service.findAdSellerByName(dao);
				toBossOneId = findAdSellerByName.get("id");
			}
		}
		if (roleid == 3) {
			toBossThreeId = sellerId;
			String storeId = adminSeller.get("storeid");
			toBossTwoId = service.findStoreId(storeId);
			if (StrKit.notBlank(dao)) {
				AdminSeller findAdSellerByName = service.findAdSellerByName(dao);
				toBossOneId = findAdSellerByName.get("id");
			}
		}
		if (roleid == 4) {
			toBossThreeId = sellerId;
			String storeId = adminSeller.get("storeid");
			toBossTwoId = service.findStoreId(storeId);
			toBossOneId = adminSeller.get("id");
		}
		
		String taici=getPara("taici");
		String state=getPara("state");
		
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0, service.findByPage(pageNum, pageSize, phone, card, sBeginDate, sEndDate, taici, state,toBossOneId, toBossTwoId, toBossThreeId));
	}
	/**
	 * 发送消息
	 */
	public void message() {
		String ids = "";
		String phone = getPara("phone");
		String card = getPara("card");
		String sBeginDate = getPara("sBeginDate");// 生日
		String sEndDate = getPara("sEndDate");
		String beginDate = getPara("beginDate");// 办卡
		String endDate = getPara("endDate");
		String store=getPara("store");
		String title="";
		if(StrKit.notBlank(store)){
			 List<Store> st=service.findStoreByIds(store);
			 for (int i = 0; i < st.size(); i++) {
				if(i==0){
					title=st.get(i).get("title");
				}else{
					title+="','"+st.get(i).get("title");
				}
			}
		}
		String dao=getPara("dao");
		String taici=getPara("taici");
		String state=getPara("state");
		List<Record> list = service.findMesageList(phone, card, sBeginDate, sEndDate, beginDate, endDate, title, dao,
				taici, state);
		for (int i = 0; i < list.size(); i++) {
			if (i == 0) {
				ids = list.get(i).get("phone");
			} else {
				ids += "," + list.get(i).get("phone");
			}
		}
		setAttr("ids", ids);
		render("sendMessage.jsp");
	}
	/**
	 * 会员积分
	 */
	public void vipindex() {
		int userid = getCurrentUser().getInt("id");
		int RoleId = service.findByRoleId(userid);
		setAttr("RoleId", RoleId);
		int VipPower = 0;
		if (RoleId == 1) {
			VipPower = 1;
			List<Store> storeList = service.findStoreList();
			setAttr("storeList", storeList);
		} else if (RoleId == 2) {
			if(StrKit.notNull(service.findByVipRole(userid))){
				VipPower = service.findByVipRole(userid).get("VipPower");
				List<Store> storeList = service.findStoreList(userid);
				setAttr("storeList", storeList);
			}
		} else{
			if(StrKit.notNull(service.findByVipRole(userid))){
				VipPower = service.findByVipRole(userid).get("VipPower");
				List<Store> storeList = service.findOneStore(userid);
				setAttr("storeList", storeList);
			}
		}
		/*List<AdSeller> findAdSeller = service.findAdSeller();
		setAttr("findAdSeller", findAdSeller);*/
		setAttr("VipPower", VipPower);
		render("vipindex.jsp");
	}
	/**
	 * 会员积分统计
	 */
	public void vippage() {
		int adminSellerid = getCurrentUser().getInt("id");
		AdminSeller adminSeller = getCurrentUser();
		int sellerId = 0;
		if (adminSeller.get("storeid") == null) {
			Seller seller = service.findSellerByUserId(adminSellerid);
			sellerId = seller.get("id");
		}
		if (adminSeller.get("storeid") != null) {
			String storeid = adminSeller.get("storeid");
			Store store = service.findSellerByStoreid(storeid);
			String sellerId2 = store.get("sellerId");
			Seller seller = service.findselleridBysellerId(sellerId2);
			sellerId = seller.get("id");
		}
		int pageNum = getParaToInt(0, 1);
		String phone = getPara("phone");
		String card = getPara("card");
		String storeid = getPara("store");
		//公司id
		int toBossOneId = 0;
		int toBossTwoId = 0;
		int toBossThreeId = 0;
		int roleid = service.findRoleidByAdminSellerId(adminSellerid);
		if (roleid == 2) {
			toBossThreeId = sellerId;
			if (StrKit.notBlank(storeid)) {
				toBossTwoId = service.findStoreId(storeid);
			}
		}
		if (roleid == 3) {
			toBossThreeId = sellerId;
			String storeId = adminSeller.get("storeid");
			toBossTwoId = service.findStoreId(storeId);
		}
		if (roleid == 4) {
			toBossThreeId = sellerId;
			String storeId = adminSeller.get("storeid");
			toBossTwoId = service.findStoreId(storeId);
			toBossOneId = adminSeller.get("id");
		}

		logger.info("toBossThreeId>>>"+toBossThreeId);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0, service.findByVipPage(pageNum, pageSize, phone, card, toBossTwoId,toBossThreeId,toBossOneId));
	}
	/**
	 * 会员积分统计 积分排序
	 */
	public void vipsjj() {
		int adminSellerid = getCurrentUser().getInt("id");
		AdminSeller adminSeller = getCurrentUser();
		int sellerId = 0;
		if (adminSeller.get("storeid") == null) {
			Seller seller = service.findSellerByUserId(adminSellerid);
			sellerId = seller.get("id");
		}
		if (adminSeller.get("storeid") != null) {
			String storeid = adminSeller.get("storeid");
			Store store = service.findSellerByStoreid(storeid);
			String sellerId2 = store.get("sellerId");
			Seller seller = service.findselleridBysellerId(sellerId2);
			sellerId = seller.get("id");
		}
		int pageNum = getParaToInt(0, 1);
		String phone = getPara("phone");
		String card = getPara("card");
		String storeid = getPara("store");
		//公司id
		int toBossOneId = 0;
		int toBossTwoId = 0;
		int toBossThreeId = 0;
		int roleid = service.findRoleidByAdminSellerId(adminSellerid);
		if (roleid == 2) {
			toBossThreeId = sellerId;
			if (StrKit.notBlank(storeid)) {
				toBossTwoId = service.findStoreId(storeid);
			}
		}
		if (roleid == 3) {
			toBossThreeId = sellerId;
			String storeId = adminSeller.get("storeid");
			toBossTwoId = service.findStoreId(storeId);
		}
		if (roleid == 4) {
			toBossThreeId = sellerId;
			String storeId = adminSeller.get("storeid");
			toBossTwoId = service.findStoreId(storeId);
			toBossOneId = adminSeller.get("id");
		}
		int date = getParaToInt("date", 0);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0, service.findByVipPages(pageNum, pageSize, phone, card, date ,toBossTwoId,toBossThreeId,toBossOneId));
	}
	/**
	 * 手机短信
	 */
	public void sendMessage() {
		String ids = getPara(0);
		setAttr("ids", ids);
		render("sendMessage.jsp");
	}
	public void doSendMessage() {
		String ids = getPara("ids");
		String message = getPara("message");
		List<String> phoneList = new ArrayList<String>();
		int subLen = 100 * 12;
		int xh = 0;//循环次数
		int a = ids.length() % subLen;
		xh = ids.length()/subLen;
		if(a > 0) {
			xh += 1;
		}
		for(int i = 0; i < xh; i++){
			if(i==xh-1){
				phoneList.add(ids.substring(i*subLen));
			}else{
				phoneList.add(ids.substring(i*subLen, i*subLen + subLen-1));
			}
		}
		for (String phone : phoneList) {
			SMSKit.sendSms(phone, message);
		}
		renderSuccess();
	}
	/**
	 * 添加会员
	 */
	public void toAdd() {
		AdminSeller adminSeller = getCurrentUser();
		int userid = getCurrentUser().getInt("id");
		int RoleId = service.findByRoleId(userid);
		int VipPower = 0;
		System.out.println(RoleId+"RoleIdRoleIdRoleId");
		setAttr("RoleId", RoleId);
		if (RoleId == 1) {
			VipPower =1;
			List<Store> storeList = service.findStoreList();
			setAttr("storeList", storeList);
		} else if (RoleId == 2) {
			String sellerId = null;
			if (StrKit.notNull(service.findByUserId(userid))&&StrKit.notNull(service.findByVipRole(userid))) {
				sellerId = service.findByUserId(userid).get("sellerId");
			}
			List<Store> storeList = service.findStoreList(sellerId);
			VipPower = service.findByVipRole(userid).get("VipPower");
			setAttr("storeList", storeList);
		} else if (RoleId == 3) {
			VipPower = service.findByVipRole(userid).get("VipPower");
			String storeid = adminSeller.get("storeid");
			Store store = service.findStoreById(storeid);
			setAttr("store", store);
			List<Record> chooseDao = service.chooseDao(storeid);
			setAttr("chooseDao", chooseDao);
		}  else if (RoleId == 4) {
			String storeid = adminSeller.get("storeid");
			Store store = service.findStoreById(storeid);
			setAttr("store", store);
			VipPower = service.findByVipRole(userid).get("VipPower");
			setAttr("adminSeller", adminSeller);
		}else{
			String storeid = null;
			if (StrKit.notNull(service.findBystroeid(userid))&&StrKit.notNull(service.findByVipRole(userid))) {
				storeid = service.findBystroeid(userid).get("storeid");
			}
			List<Store> storeList = service.findStoreidList(storeid);
			VipPower = service.findByVipRole(userid).get("VipPower");
			setAttr("storeList", storeList);
		}
		setAttr("VipPower", VipPower);
		render("add.jsp");
	}

	/**
	 * 添加
	 * 
	 * @return -1：异常，0：成功，1：用户名为空，2：用户名格式不正确，3：用户已经存在，4：密码为空，5：密码格式不正确，6：密码长度不正确
	 *         ，7：昵称为空，8：昵称过长
	 */
	public void doAdd(){
		//电话
		String aphone = getPara("aphone");
		//姓名
		String aname = getPara("aname");
		//性别
		int sex = getParaToInt("radio");
		String birthday = getPara("birthday");
		String yuchan = getPara("yuchanthday");
		String babythday = getPara("babythday");
		int taici = getParaToInt("taici");
		String province = getPara("province");
		String city = getPara("city");
		String area = getPara("area");
		String address = getPara("address");
		
		String storeid = getPara("store");
		
		int stid = 0;
		if (StrKit.notBlank(storeid)) {
			Store store = service.findStoreById1(storeid);
			stid = store.get("id");
		}	
		String toBossOneId = getPara("toBossOneId");
		int state = getParaToInt("state");
		String addressa=province+city+area;
		if(StrKit.notBlank(address)){
			addressa=addressa+address;
		}
		User newUser=service.findByPhone(aphone);
		if (newUser!=null) {
			renderFailed("手机号已经存在");
			return;
		}
		String cards=DatetimeKit.getFormatDate("yyyyMMddHHmmssSSS");
		String card=cards.substring(2);
		User user=new User();
		//String date= DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss");
		//设置用户密码，默认为手机号后六位
		String password = aphone.substring(aphone.length() - 6);
		int userid = getCurrentUser().get("id");
		Seller seller = service.findSellerByAdminId(userid);
		int toBossThreeId = seller.get("id");
		user.set("name", aname)
		 	.set("card", card)
		 	.set("phone", aphone)
		 	.set("password", CommonKit.encryptPassword(password))
		 	.set("sex", sex)
		 	.set("birthday", birthday)
		 	.set("state", state)
		 	.set("address", addressa)
		 	.set("toBossTwoId", stid)
		 	.set("toBossThreeId", toBossThreeId)//所属公司
		 	.set("createDate", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"));
		if(StrKit.notBlank(yuchan)){
			user.set("babythday", yuchan);
		}
		if(StrKit.notBlank(toBossOneId)){
			user.set("toBossOneId", toBossOneId);
		}
		if(StrKit.notBlank(babythday)){
			user.set("babythday", babythday);
		}
		if(taici !=0){
			user.set("taici", taici);
		}
		if(service.addUser(user)){
			renderSuccess();
			return;
		}
		renderFailed();
	}
	/**
	 * 会员详情
	 */
	public void detail() {
		int id = getParaToInt(0, 0);
		if (id != 0) {
			User user = service.findById(id);
			setAttr("user", user);
			List<Store> storeList = service.findStoreList();
			setAttr("storeList", storeList);
			List<Record> userList = service.findUserList(user.get("store"));
			setAttr("userList", userList);
		}
		render("detail.jsp");
	}
	/**
	 * 会员积分详情
	 */
	public void detailVip() {
		try {
			int id = getParaToInt(0, 0);
			if (id != 0) {
				User user = service.findById(id);
				setAttr("user", user);
				int toBossTwoId = user.get("toBossTwoId");
				Store store = service.findStoreBytoBossTwoId(toBossTwoId);
				setAttr("store", store);
				/* 线下积分消费明细 */
				List<Record> offJfOrderList = service.findOffJfOrderList(user.get("phone"));
				System.out.println(offJfOrderList);
				setAttr("offJfOrderList", offJfOrderList);
				/* 线下订单明细 */
				/*List<Record> offOrderList = service.findOffOrderList(user.get("phone"));
				setAttr("offOrderList", offOrderList);
				double sppoint = SysConfig.getSpoint();
				double jkpoint = SysConfig.getKpoint();
				double jypoint = SysConfig.getJpoint();
				setAttr("sppoint", sppoint);
				setAttr("jkpoint", jkpoint);
				setAttr("jypoint", jypoint);*/
				/* 线上订单明细 */
				List<Record> orderList = service.findOrderList(id);
				System.out.println(orderList);
				setAttr("orderList", orderList);
			}
			render("detailVip.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 验证手机号
	 */
	public void validate() {
		String phone = getPara("aphone");
		User user = service.findByPhone(phone);
		if (StrKit.notNull(user)) {
			renderFailed();
			return;
		} else {
			renderSuccess();
			return;
		}
	}
	/**
	 * 选择营业部
	 */
	public void chooseDao(){
		String[] store = getParaValues("store");
		String str = java.lang.String.join("','", store);
		renderSuccess(service.chooseDao(str));
	}
	/**
	 * 编辑会员
	 */
	public void doEdit() {
		int userid = getParaToInt("userid", 0);
		String sppoint = getPara("sppoint");
		String jypoint = getPara("jypoint");
		String jkpoint = getPara("jkpoint");
		String name = getPara("name");
		String phone = getPara("phone");
		String store = getPara("store");
		String toBossOneId = getPara("toBossOneId");
		if (userid != 0) {
			User user = service.findById(userid);
			if (StrKit.notNull(user)) {
				if (StrKit.notBlank(phone)) {
					if (!user.get("phone").equals(phone)) {
						User users = service.findByPhone(phone);
						if (StrKit.notNull(users)) {
							renderResult(2);
							return;
						} else {
							user.set("phone", phone);
						}
					}
				}
				if (StrKit.isBlank(name)) {
					renderResult(3);
				}else {
					user.set("name", name);
				}
				if (StrKit.notBlank(jypoint)) {
					user.set("jypoint", jypoint);
				}
				if (StrKit.notBlank(sppoint)) {
					user.set("sppoint", sppoint);
				}
				if (StrKit.notBlank(jkpoint)) {
					user.set("jkpoint", jkpoint);
				}
				if (StrKit.notBlank(store)) {
					Store st = service.findStoreById(store);
					user.set("toBossTwoId", st.get("id"));
				}
				if (StrKit.notBlank(store) && StrKit.notBlank(toBossOneId)) {
					user.set("toBossOneId", toBossOneId);
				} else if (StrKit.notBlank(store) && StrKit.isBlank(toBossOneId)) {
					user.set("toBossOneId", "");
				}
				if (user.update()) {
					//service.setPhone(phone, oldPhone);
					renderSuccess(service.findById(userid));
					return;
				} else {
					renderFailed();
					return;
				}
			} else {
				renderFailed();
				return;
			}
		} else {
			renderFailed();
			return;
		}
	}
	/**
	 * excel导出
	 */
	public void doVipExcel() {
		List<Pair> titles = new ArrayList<Pair>();
		titles.add(new Pair("card", "会员卡号"));
		titles.add(new Pair("name", "会员姓名"));
		titles.add(new Pair("phone", "会员手机号"));
		titles.add(new Pair("storeName", "所属分公司"));
		titles.add(new Pair("createDate", "办卡日期"));
		titles.add(new Pair("sppoint", "积分"));
		titles.add(new Pair("jkpoint", "抵扣券"));
		titles.add(new Pair("jypoint", "惠券"));
		String phone = getPara("phone");
		String card = getPara("card");
		String store = getPara("store");
		String title = "";
		if (StrKit.notBlank(store)) {
			Store st = service.findStoreById(store);
			title = st.getStr("title");
		}
		int userid = getCurrentUser().getInt("id");
		int RoleId = service.findByRoleId(userid);
		String storeid = null;
		String sellerId = null;
		if (RoleId == 2 && StrKit.notNull(service.findByUserId(userid))) {
			sellerId = service.findByUserId(userid).get("sellerId");
		}
		if (RoleId == 3 || RoleId == 4 && StrKit.notNull(service.findBystroeid(userid))) {
			storeid = service.findBystroeid(userid).get("storeid");
			sellerId=service.findBysellerId(storeid).get("sellerId");
		}
		List<Record> list = service.findExcelVipByPage(phone, card, title,sellerId);
		List<Record> listz = new ArrayList<Record>();
		for (int i = 0; i < list.size(); i++) {
			Record record = new Record();
			record.set("card", list.get(i).get("card"));
			record.set("name", list.get(i).get("name"));
			record.set("phone", list.get(i).get("phone"));
			record.set("storeName", list.get(i).get("storeName"));
			record.set("createDate", list.get(i).get("createDate"));
			record.set("sppoint", list.get(i).get("sppoint"));
			record.set("jkpoint", list.get(i).get("jkpoint"));
			record.set("jypoint", list.get(i).get("jypoint"));
			listz.add(record);
		}
		// 导出
		service.exportVip(getResponse(), getRequest(), titles, listz);
		renderNull();
	}

	/**
	 * excel导出
	 */
	public void doExcel() {
		List<Pair> titles = new ArrayList<Pair>();
		titles.add(new Pair("card", "会员卡号"));
		titles.add(new Pair("name", "会员姓名"));
		titles.add(new Pair("phone", "会员手机号"));
		titles.add(new Pair("storeName", "所属分公司"));
	    titles.add(new Pair("trueName", "营业部"));
		titles.add(new Pair("userNum", "团队人数"));
	    titles.add(new Pair("pareName", "邀请人"));
		titles.add(new Pair("createDate", "办卡日期"));
		titles.add(new Pair("birthday", "会员生日"));
		titles.add(new Pair("state", "会员状态"));
		titles.add(new Pair("taici", "等级"));
		titles.add(new Pair("babythday", "升级日期"));
		titles.add(new Pair("countnum", "线上消费次数"));
		
		titles.add(new Pair("address", "会员地址"));
		titles.add(new Pair("guanzhu", "关注"));

		int userid = getCurrentUser().getInt("id");
		int RoleId = service.findByRoleId(userid);
		String phone = getPara("phone");
		String card = getPara("card");
		String sBeginDate = getPara("sBeginDate");// 生日
		String sEndDate = getPara("sEndDate");
		String beginDate = getPara("beginDate");// 办卡
		String endDate = getPara("endDate");
		String store=getPara("store");
		String address=getPara("address");
		String title="";
		if(StrKit.notBlank(store)){
			 List<Store> st=service.findStoreByIds(store);
			 for (int i = 0; i < st.size(); i++) {
				if(i==0){
					title=st.get(i).get("title");
				}else{
					title+="','"+st.get(i).get("title");
				}
			}
		 }
		String dao=getPara("dao");
		String taici=getPara("taici");
		String state=getPara("state");
		String storeid = null;
		String sellerId = null;
		if (RoleId == 2 && StrKit.notNull(service.findByUserId(userid))) {
			sellerId = service.findByUserId(userid).get("sellerId");
		}
		if (RoleId == 3 || RoleId == 4 && StrKit.notNull(service.findBystroeid(userid))) {
			storeid = service.findBystroeid(userid).get("storeid");
			sellerId=service.findBysellerId(storeid).get("sellerId");
		}
		
		List<Record> list = service.findExcelByPage(phone, card, sBeginDate, sEndDate, beginDate, endDate,address,title, dao,
				taici, state,  storeid, sellerId);
		List<Record> listz = new ArrayList<Record>();
		for (int i = 0; i < list.size(); i++) {
			Record record = new Record();
			record.set("card", list.get(i).get("card"));
			record.set("name", list.get(i).get("name"));
			record.set("phone", list.get(i).get("phone"));
			record.set("birthday", list.get(i).get("birthday"));
			if (list.get(i).get("state").equals(1)) {
				record.set("state", "未登录");
			} else if (list.get(i).get("state").equals(2)) {
				record.set("state", "未注册");
			} else if (list.get(i).get("state").equals(3)) {
				record.set("state", "会员");
			}
			if(list.get(i).get("taici").equals(1)){
				record.set("taici", "会员");
			}else if(list.get(i).get("taici").equals(2)){
				record.set("taici", "黄金");
			}else if(list.get(i).get("taici").equals(3)){
				record.set("taici", "白金");
			}else if(list.get(i).get("taici").equals(4)){
				record.set("taici", "经纪人");
			}else if(list.get(i).get("taici").equals(5)){
				record.set("taici", "营业部");
			}else if(list.get(i).get("taici").equals(6)){
				record.set("taici", "分公司");
			}else if(list.get(i).get("taici").equals(7)){
				record.set("taici", "公司");
			}
			record.set("babythday", list.get(i).get("babythday"));
	

			record.set("allCount", list.get(i).get("allCount"));	
			
			if( list.get(i).get("excnum")==null){
				record.set("excnum","0");
			}else{
				record.set("excnum", list.get(i).get("excnum"));
			}
			record.set("countnum", list.get(i).get("countnum"));
			record.set("storeName", list.get(i).get("storeName"));
			record.set("trueName", list.get(i).get("trueName"));
			record.set("createDate", list.get(i).get("createDate"));
			record.set("address", list.get(i).get("address"));
			if (list.get(i).get("guanzhu").equals(1)) {
				record.set("guanzhu", "");
			} else if (list.get(i).get("guanzhu").equals(2)) {
				record.set("guanzhu", "是");
			}
			if(list.get(i).get("userNum")!=null){
				record.set("userNum", list.get(i).get("userNum"));
			}else{
				record.set("userNum", 0);
			}
			record.set("pareName", list.get(i).get("pareName"));
			listz.add(record);
		}
		//向登录下载统计表中添加数据 会员信息统计
	    AuthDownload downObj=downloadService.findByUserId(getCurrentUser().getInt("id"));
		AuthDownload download=new AuthDownload();
		download.set("logName","平台端")
			        .set("name", downObj.get("trueName"))
			        .set("accountRole", downObj.get("alias"))
			        .set("storeName", downObj.get("title"))
			        .set("memberDownload", 1)
                   .set("downloadDate",DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"));

		download.save();
		// 导出
		service.export(getResponse(), getRequest(), titles, listz);
		renderNull();
	}
	/**
	 * 会员积分
	 */
	public void moneyDetail() {
		//用户id
		int userid = getCurrentUser().getInt("id");
		AdminSeller adminSeller = service.findAdminSellerByid(userid);
		int tuserid = adminSeller.getInt("tokerUserid");
		//根据手机号查询当前用户的前端账号
		User user = service.findUserById(tuserid);
		if (user==null) {
			renderFailed("该用户无前端权限");
			return;
		}
		setAttr("user", user);
		//根据登陆用户的前端id查询其变动日志
		int userId = user.get("id");
		List<Record> orderList = service.findOrderList(userId);
		setAttr("orderList", orderList);
		Date date = new Date();
		Record vJypoint = service.queryViablejypoint(userId, date);
		setAttr("vJypoint", vJypoint);
		//查询转换比例s0014
		String value = service.findSysConfig();
		setAttr("value", value);
		System.out.println(value);
		render("moneyDetail.jsp");
	}
	/**
	 * 惠券转换余额
	 */
	public void exchangeMoney() {
		//获取可转换的余额
		int userid=getParaToInt("userid");
		User user = service.findById(userid);
		Double jypoint = user.getDouble("jypoint");
		//查询转换比例s0014
		String svalue = service.findSysConfig();
		int value = Integer.parseInt(svalue);
		double money1 = DoubleKit.mul(jypoint, value);
		double money = DoubleKit.div(money1, 100, 2);
		if (StrKit.isBlank(svalue)) {
			renderFailed("兑换比例异常");
			return;
		}
		if (money<=0) {
			renderFailed("可兑换金额不能为0");
			return;
		}
		//更改用户财富信息并记录表
		Record re = new Record();
		String orderNumStore = "YE"+DatetimeKit.getFormatDate("yyyyMMddHHmmssSSS")+CommonKit.buildRandomNum(3);
		re.set("userid", getCurrentUser().getInt("id"))
		.set("type",2)
		.set("flag",12)
		.set("fromOrderNum",orderNumStore)
		.set("data",1)
		.set("describe","惠券转换余额");
		System.out.println(userid);
		try {
			if (service.exchangeMoney(user,jypoint,money,orderNumStore,re,2)) {
				renderSuccess();
			}else {
				renderFailed("网络繁忙...，请稍后再试");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			renderFailed("网络繁忙...，请稍后再试1");
		}
	 /* 
	  
	  
	  Double jypoint=Double.valueOf(ecoupon)/Double.valueOf(scale)*100;
	  Double ecoupond=Double.valueOf(ecoupon);
	     try {
	   if (service.exchangeMoney(getCurrentUser(),jypoint,ecoupond,orderNumStore,re,2)) {
	    renderSuccess();
	   }else {
	    renderFailed("网络繁忙...，请稍后再试");
	   }
	  } catch (Exception e) {
	   // TODO Auto-generated catch block
	   e.printStackTrace();
	   renderFailed("网络繁忙...，请稍后再试1");
	  } */
	 }
}